package com.cloud.base.proxy;


import com.cloud.base.bean.OpenConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;


@Component
public class ApiFactory implements InitializingBean, ApplicationContextAware {

    private DefaultListableBeanFactory beanFactory = null;

    private ApplicationContext applicationContext;


    public static <T> T create (Class<T> apiClass, OpenConfig openConfig) {
        InvocationHandler invocationProxy = createProxy(openConfig);
        T bean = (T) Proxy.newProxyInstance(ApiFactory.class.getClassLoader(), new Class[]{apiClass}, invocationProxy);
        //BEAN_MAP.put(key, bean);
        return bean;
    }

    static InvocationHandler createProxy(OpenConfig openConfig) {
        ApiInvocationProxy invocationProxy = new ApiInvocationProxy(openConfig);
        return invocationProxy;
    }

    public static <T> T create (Class<T> apiClass) {
        InvocationHandler invocationProxy = createProxy();
        T bean = (T) Proxy.newProxyInstance(ApiFactory.class.getClassLoader(), new Class[]{apiClass}, invocationProxy);
        //BEAN_MAP.put(key, bean);
        return bean;
    }

    static InvocationHandler createProxy() {

        return new ApiInvocationProxy();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(this.applicationContext == null){
            this.applicationContext = applicationContext;
        }
/*        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
        beanFactory = (DefaultListableBeanFactory) configurableApplicationContext
                .getBeanFactory();*/

    }


/*    @PostConstruct
    public void init() {
        Test test = new TestImpl();
        inject(test.getClass());
    }

    private void inject(Class clazz){
        try {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz.getClass());
            beanFactory.registerBeanDefinition(clazz.getName(),
                    builder.getBeanDefinition());
            //logger.info("remote 注册"+classzz.getName()+"到Spring容器中...");
        } catch (Exception e) {
            throw new RuntimeException("创建service" + "异常:", e);
        }

    }*/

    @Override
    public void afterPropertiesSet() throws Exception {
//        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
//        Test test = new TestImpl();
//        ac.getBeanFactory().registerSingleton(test.getClass().getName(),test.getClass());
        //将扫描到的类注册到spring容器
        //ac.register(AppConfig.class);
        //刷新容器，完成bean的实例化
     //   ac.refresh();
        //ac.getBean(ApiFactory.class).showEvanServiceResult();

    }



}
